<template>
  <div>
    <el-dialog :title="TypeForm.typeId?'修改信息':'添加信息'" :visible.sync="visible">
      <el-form :model="TypeForm" label-width="80px" :rules="rules" ref="salesDom">
        <el-form-item label="分类名" prop="typeId">
          <el-input v-model="TypeForm.name"  placeholder="分类名"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="TypeForm.status">
            <el-radio :label="0">异常</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="visible=false">取消</el-button>
          <el-button type="primary" @click="submitInfo">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const initInfo = {
  typeId: '',
  name: '',
  status: 1
}

export default {
  name: 'TypeAddOrUpdate',
  data () {
    return {
      visible: false,
      TypeForm: {
        ...initInfo
      },
      rules: {
        name: [
          { required: true, message: '分类名不能为空', trigger: 'blur' }
        ]

      },
      goodsList: [],
      loading: ''
    }
  },
  methods: {
    init (typeId) {
      /* 开启弹窗 */
      this.visible = true
      this.TypeForm = { ...initInfo }
      this.TypeForm.typeId = typeId

      /* 判断userId是否有值 */
      if (typeId) {
        /* 成立表示该次操作为修改，发送ajax请求获取信息 */
        this.$http.get('/type/select/' + typeId).then(data => {
          if (data.code === 0) {
            this.TypeForm = {
              ...data.list
            }
          } else {
            this.$message.error('数据加载失败，请重试！')
          }
        })
      }
    },
    submitInfo () {
      this.$refs.salesDom.validate(valid => {
        if (valid) {
          this.loading = this.$loading({ text: '加载中...' })
          this.$http.post('/type/update', this.TypeForm).then(data => {
            this.loading.close()
            if (data.code === 0) {
              this.$message({
                message: `${!this.TypeForm.typeId ? '添加' : '修改'}成功`,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('onCloseWin')
                }
              })
            } else {
              this.$alert(data.msg, { type: 'error' })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
